package hedron.editor;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.DetailsPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.MasterDetailsBlock;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.EditorPart;

public class __DemoEditor1 extends EditorPart {
	public static final String ID = "HEDRON.ContactsEditor";
	private FormToolkit toolkit;
	private ScrolledForm form;

	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		setSite(site);
		setInput(input);
	}

	public void dispose() {
		toolkit.dispose();
		super.dispose();
	}

	@Override
	public void createPartControl(Composite parent) {
		toolkit = new FormToolkit(parent.getDisplay());
		form = toolkit.createScrolledForm(parent);
		{
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			form.getBody().setLayout(layout);
		}

		{// contacts section
			Section section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
			{
				section.setText("Contacts");
				section.marginWidth = 0;
				section.marginHeight = 0;
				GridData gdata = new GridData(GridData.FILL_BOTH);
				gdata.horizontalSpan = 1;
				section.setLayoutData(gdata);
			}

			{
				Composite client = toolkit.createComposite(section, SWT.WRAP);
				section.setClient(client);
				toolkit.paintBordersFor(client);

				GridLayout layout = new GridLayout();
				layout.numColumns = 1;
				layout.marginWidth = 2;
				layout.marginHeight = 2;
				client.setLayout(layout);

				{// cell 1
					Table table = toolkit.createTable(client, SWT.NULL);
					GridData gdata = new GridData(GridData.FILL_BOTH);
					table.setLayoutData(gdata);

				}
				{// cell 2
					Button b = toolkit.createButton(client, "Add", SWT.PUSH); //$NON-NLS-1$
					GridData gdata = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
					gdata.heightHint = 20;
					// gdata.widthHint = 100;
					b.setLayoutData(gdata);
				}
			}
		}

		{// detail section
			Section section = toolkit.createSection(form.getBody(), /*
																	 * Section.DESCRIPTION
																	 */Section.TITLE_BAR);
			{
				section.setText("Details");
				section.marginWidth = 0;
				section.marginHeight = 0;
				GridData gdata = new GridData(GridData.FILL_BOTH);
				gdata.widthHint = 100;
				section.setLayoutData(gdata);
			}

			{
				Composite client = toolkit.createComposite(section, SWT.WRAP);
				section.setClient(client);
				toolkit.paintBordersFor(client);

				GridLayout layout = new GridLayout();
				layout.numColumns = 2;
				layout.marginWidth = 2;
				layout.marginHeight = 2;
				client.setLayout(layout);

				{// line 1
					Label label = toolkit.createLabel(client, "name:");
					GridData gdata = new GridData(GridData.END, GridData.CENTER, false, false);
					label.setLayoutData(gdata);

					Text text = toolkit.createText(client, "George Radev");
					gdata = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
					text.setLayoutData(gdata);
				}
				{// line 2
					Label label = toolkit.createLabel(client, "address:");
					GridData gdata = new GridData(GridData.END, GridData.CENTER, false, false);
					label.setLayoutData(gdata);

					Text text = toolkit.createText(client, "Sofia");
					gdata = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
					text.setLayoutData(gdata);
				}
				{// line 3
					Label label = toolkit.createLabel(client, "note:");
					GridData gdata = new GridData(GridData.END, GridData.CENTER, false, false);
					label.setLayoutData(gdata);

					Text text = toolkit.createText(client, "test");
					gdata = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
					text.setLayoutData(gdata);
				}
			}
		}
	}

	@Override
	public void setFocus() {
		form.setFocus();
	}

	@Override
	public void doSave(IProgressMonitor monitor) {}

	@Override
	public void doSaveAs() {}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		return false;
	}

	public static class ScrolledPropertiesBlock extends MasterDetailsBlock {
		private FormPage page;

		public ScrolledPropertiesBlock(FormPage page) {
			this.page = page;
		}

		/**
		 * @param id
		 * @param title
		 */
		class MasterContentProvider implements IStructuredContentProvider {
			public Object[] getElements(Object inputElement) {
				return new Object[0];
			}

			public void dispose() {}

			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
		}

		protected void createMasterPart(final IManagedForm managedForm, Composite parent) {
			// final ScrolledForm form = managedForm.getForm();
			FormToolkit toolkit = managedForm.getToolkit();
			Section section = toolkit.createSection(parent, Section.DESCRIPTION | Section.TITLE_BAR);
			section.setText("ScrolledPropertiesBlock.sname"); //$NON-NLS-1$
			section.setDescription("ScrolledPropertiesBlock.sdesc"); //$NON-NLS-1$
			section.marginWidth = 10;
			section.marginHeight = 5;
			Composite client = toolkit.createComposite(section, SWT.WRAP);
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			layout.marginWidth = 2;
			layout.marginHeight = 2;
			client.setLayout(layout);
			Table t = toolkit.createTable(client, SWT.NULL);
			GridData gd = new GridData(GridData.FILL_BOTH);
			gd.heightHint = 20;
			gd.widthHint = 100;
			t.setLayoutData(gd);
			toolkit.paintBordersFor(client);
			Button b = toolkit.createButton(client, "ScrolledPropertiesBlock.add", SWT.PUSH); //$NON-NLS-1$
			gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
			b.setLayoutData(gd);
			section.setClient(client);
			final SectionPart spart = new SectionPart(section);
			managedForm.addPart(spart);
			TableViewer viewer = new TableViewer(t);
			viewer.addSelectionChangedListener(new ISelectionChangedListener() {
				public void selectionChanged(SelectionChangedEvent event) {
					managedForm.fireSelectionChanged(spart, event.getSelection());
				}
			});
			viewer.setContentProvider(new MasterContentProvider());
			viewer.setInput(page.getEditor().getEditorInput());
		}

		protected void createToolBarActions(IManagedForm managedForm) {
			final ScrolledForm form = managedForm.getForm();
			Action haction = new Action("hor", Action.AS_RADIO_BUTTON) { //$NON-NLS-1$
				public void run() {
					sashForm.setOrientation(SWT.HORIZONTAL);
					form.reflow(true);
				}
			};
			haction.setChecked(true);
			haction.setToolTipText("ScrolledPropertiesBlock.horizontal"); //$NON-NLS-1$
			// haction.setImageDescriptor(FormArticlePlugin.getDefault().getImageRegistry().getDescriptor(FormArticlePlugin.IMG_HORIZONTAL));
			Action vaction = new Action("ver", Action.AS_RADIO_BUTTON) { //$NON-NLS-1$
				public void run() {
					sashForm.setOrientation(SWT.VERTICAL);
					form.reflow(true);
				}
			};
			vaction.setChecked(false);
			vaction.setToolTipText("ScrolledPropertiesBlock.vertical"); //$NON-NLS-1$
			// vaction.setImageDescriptor(FormArticlePlugin.getDefault().getImageRegistry().getDescriptor(FormArticlePlugin.IMG_VERTICAL));
			form.getToolBarManager().add(haction);
			form.getToolBarManager().add(vaction);
		}

		@Override
		protected void registerPages(DetailsPart detailsPart) {}
	}
}